package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.exception.SellerLoginNoPassException;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.TbSellerService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("seller")
public class SellerController {

    @Reference
    private TbSellerService tbSellerService;

    @RequestMapping("showUser")
    @ResponseBody
    public Result showUser(TbSeller user) {
        //通过调用LoginRealm进行认证
        //1.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.将我们的传来的用户名,密码包,装成传来的shiro的token对象;
        UsernamePasswordToken token = new UsernamePasswordToken(user.getSellerId(), user.getPassword());
        //3.调用subject的login方法执行登录逻辑
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            return Result.response(ResultEnum.ERROEUSER);
        } catch (IncorrectCredentialsException e) {
            return Result.response(ResultEnum.WAITPASS);
        } catch (SellerLoginNoPassException e){
            return Result.response(ResultEnum.NOPASS);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("unameShow")
    @ResponseBody
    public Result unameShow(String sellerId){
          int i  = tbSellerService.selectUname(sellerId);
          if (i == 0) {
              return Result.response(ResultEnum.OVERUNAME);
          }else {
          return Result.response(ResultEnum.SUCCESS);
      }
    }
    @RequestMapping("register")
    @ResponseBody
    public Result register(TbSeller tbSeller){
        int i = tbSellerService.addInf(tbSeller);
        return Result.response(ResultEnum.SUCCESS);
    }

}
